package com.tronsis.library.widget;

import android.support.annotation.NonNull;
import android.view.View;

/**
 * View 双击事件
 */
public class DoubleClickListener implements View.OnClickListener {

  private final long delayTime = 300;
  private long lastClickTime = 0;
  private final IDoubleClickCallback doubleClickCallbackView;

  public interface IDoubleClickCallback {
    void onDoubleClicked();
  }

  @Override
  public final void onClick(View v) {
    long nowClickTime = System.currentTimeMillis();
    if (nowClickTime - lastClickTime > delayTime) {
      lastClickTime = nowClickTime;
    } else {
      onDoubleClick(v);
    }
  }

  public DoubleClickListener(@NonNull IDoubleClickCallback doubleClickCallbackView) {
    this.doubleClickCallbackView = doubleClickCallbackView;
  }

  public void onDoubleClick(View v) {
    doubleClickCallbackView.onDoubleClicked();
  }

}